package com.example.dance.mapper;

import com.example.dance.pojo.Video;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface VideoMapper {
    @Insert("insert into dance.video (video_src, title, img_src, `desc`, type) " +
            "VALUE (#{videoSrc},#{title},#{imgSrc},#{desc},#{type})")
    @Options(useGeneratedKeys = true, keyProperty = "videoId", keyColumn = "video_id")
    int insertNewVideo(Video video);

    @Results({
            @Result(property = "videoId", column = "video_id"),
            @Result(property = "videoSrc", column = "video_src"),
            @Result(property = "title", column = "title"),
            @Result(property = "imgSrc", column = "img_src"),
            @Result(property = "desc", column = "desc"),
    })
    @Select("select video_id,title,img_src,type from dance.video " +
            "where video_id<=#{currentKeyset} " +
            "order by video_id desc limit #{begin},#{end}")
    List<Video>  getAllVideosList(@Param("begin") int begin,
                                  @Param("currentKeyset") int currentKeyset,
                                  @Param("end") int end);

    @Results({
            @Result(property = "videoId", column = "video_id"),
            @Result(property = "videoSrc", column = "video_src"),
            @Result(property = "title", column = "title"),
            @Result(property = "imgSrc", column = "img_src"),
            @Result(property = "desc", column = "desc"),
    })
    @Select("select video_id,title,img_src,type from dance.video " +
            "where type=#{type} and (video_id<=#{currentKeyset})" +
            "order by video_id desc limit #{begin},#{end}")
    List<Video>  getVideosListByType(
            @Param("type") int type,
            @Param("begin") int begin,
            @Param("currentKeyset") int currentKeyset,
            @Param("end") int end);

    @Results({
            @Result(property = "videoId", column = "video_id"),
            @Result(property = "videoSrc", column = "video_src"),
            @Result(property = "title", column = "title"),
            @Result(property = "imgSrc", column = "img_src"),
            @Result(property = "desc", column = "desc"),
    })
    @Select("select * from dance.video " +
            "where video_id=#{videoId}")
    Video getVideoById(@Param("videoId") int videoId);

    @Select("select count(*) from dance.video")
    int getVideoCount();
}
